package com.example.smallwork;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;

import com.example.smallwork.ui.login.LoginActivity;

public class MainActivity extends AppCompatActivity {
    private String times=null;
    private String dates=null;
    Jilu jiu=new Jilu();
    Dbhelper dbhelper = null;
    private TextView ming;
    List list = new ArrayList();

//    Reader reader = null;
//    LibraryDBHelper dbHelper = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //startActivity(new Intent(MainActivity.this, Login.class));
        setContentView(R.layout.activity_main);
        Intent intent = getIntent();
        String username=intent.getStringExtra("user");
        jiu.setUsername(username);
        dbhelper = new Dbhelper(getApplicationContext());
        TextView ming = findViewById(R.id.ming);
        ming.setText(username);
        //Toast.makeText(MainActivity.this,username,Toast.LENGTH_SHORT).show();
        ImageButton playBtn = findViewById(R.id.playBtn);//获取“播放/停止”按钮
//启动服务与停止服务，实现播放背景音乐与停止播放背景音乐

        playBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//判断音乐播放的状态
                if (MusicService.isplay == false) {
//启动服务，从而实现播放背景音乐
                    startService(new Intent(MainActivity.this, MusicService.class));
//更换播放背景音乐图标
                    ((ImageButton) v).setImageDrawable(getResources().getDrawable(R.drawable.a, null));
                } else {
//停止服务，从而实现停止播放背景音乐
                    stopService(new Intent(MainActivity.this, MusicService.class));
//更换停止背景音乐图标
                    ((ImageButton) v).setImageDrawable(getResources().getDrawable(R.drawable.a, null));
                }
            }
        });
    }
    //实现进入界面时，启动背景音乐服务
    @Override
    protected void onStart() {
//启动服务，从而实现进入应用时就播放背景音乐
        startService(new Intent(MainActivity.this, MusicService.class));
        super.onStart();
    }

    public void insert(View view) {
        hide();
        xuanze();
    }
    public void look(View view) {
        see();
    }
    public void shanchu(View view) {
        shanchu();
    }
    public void xuanze() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss 获取当前时间
        final Date date = new Date(System.currentTimeMillis());
        times=simpleDateFormat.format(date);//收集当前系统时间
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择今天的运动情况");
        final String items[] = {"无运动", "跑步1公里", "跑步5公里", "跑步10公里","跳绳10分钟","其他运动（30分钟计算打卡一次）"};
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String item = items[which];
                if (item!=null){
                    dates=times+"   "+item;
                    list.add(dates);
                    //Toast.makeText(MainActivity.this,"对的"+dates,Toast.LENGTH_SHORT).show();
                     jiu.setJi(dates);
                    //Toast.makeText(MainActivity.this,"对不的"+jiu.getJi()+"重金为得到"+jiu.getUsername(),Toast.LENGTH_SHORT).show();
                    //利用数据库帮助类对象实现插入
                    dbhelper.insertjl(jiu);
                    //Toast.makeText(MainActivity.this,"对sadasfdf的"+dates,Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();
            }
        });
        builder.show();
    }
    @SuppressLint("Range")
    public void see(){
        LinearLayout it1= (LinearLayout) findViewById(R.id.lay);
        it1.removeAllViews();
        Cursor cursor = dbhelper.queryjilu(jiu);
//显示结果
        int count = cursor.getCount();
        String str = "";
        int i = 0;
        list.clear();
//针对游标 cursor 中的每一行循环
        while (cursor.moveToNext()) {
//构造和装配一个 jilu 对
            Jilu jiz=new Jilu();
               jiz.setJi(cursor.getString(cursor.getColumnIndex("ji")));

                list.add(jiz.getJi());
            TextView tv = new TextView(this);
            StringBuilder idate = new StringBuilder();
            idate.append(list.get(i));
            tv.setText(idate);
            tv.setTextSize(15);
            tv.setGravity(Gravity.LEFT);
            it1.addView(tv);
            i++;
//将构造的对象转为字符串
            }
        it1.setVisibility(View.VISIBLE);
    }
    public void hide(){
        LinearLayout it2= (LinearLayout) findViewById(R.id.lay);
        it2.setVisibility(View.GONE);
    }
    public void shanchu(){
        boolean empty = list.isEmpty();
        if(empty){
            Toast.makeText(getApplicationContext(),"非法操作！".toString(), Toast.LENGTH_LONG).show();
            return;
        }
        else {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("提示");
            builder.setMessage("你的一条打卡记录已删除！");
            builder.setPositiveButton("好的", null);
            builder.show();
            if (list.size()>1){
                String s=list.get(list.size()-1).toString();
                jiu.setJi(s);
                dbhelper.deletejil(jiu);
                list.remove((list.size()-1));
                see();
                return;
            }
            if(list.size()==1){
                String s=list.get(list.size()-1).toString();
                jiu.setJi(s);
                dbhelper.deletejil(jiu);
                dbhelper.deletejil(jiu);
                list.remove(0);
                see();
                return;
            }
        }
    }
}
